package net.towee.client.com.lobby.presenter;

import net.towee.client.com.lobby.Lobby;
import net.towee.client.com.lobby.LobbyStep;
import net.towee.client.com.stuff.presenter.StuffPresenter;
import net.towee.client.event.core.UserEnteredToGroupEvent;
import net.towee.client.event.core.UserEnteredToGroupHandler;
import net.towee.client.gadgets.ads.AdsGadgetInfo;
import net.towee.client.gadgets.recentdocs.RecentDocumentsGadgetInfo;
import net.towee.client.managers.DashBoardManager;
import net.towee.client.managers.GroupsManager;
import net.towee.model.account.AccountGroup;
import net.towee.model.account.Board;
import net.towee.model.account.Gadget;
import net.towee.model.account.impl.BoardImpl;
import net.towee.model.account.impl.GadgetImpl;
import net.towee.model.group.GroupIdentity;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.google.inject.Singleton;

@Singleton
public class LobbyGroupsPresenter extends StuffPresenter implements LobbyStep {

	public interface Display extends StuffPresenter.Display {
		
		HasClickHandlers getNextButton();
		
		HasClickHandlers getCloseButton();
		
	}

	// Injected
	private final Lobby lobby;
	private final HandlerManager eventBus;
	private final DashBoardManager boardsManager;
	private final GroupsManager groupsManagre;
	
	private Board board = null;
	
	@Inject
	public LobbyGroupsPresenter(Lobby lobby, Display display, 
			LobbyGroupsFinder finder, LobbyGroupsCreator creator, HandlerManager eventBus,
			DashBoardManager boardsManager, GroupsManager groupsManager) {
		super(display, finder, creator);
		this.lobby = lobby;
		this.eventBus = eventBus;
		this.boardsManager = boardsManager;
		this.groupsManagre = groupsManager;
		
		display.getNextButton().addClickHandler(this);
		display.getCloseButton().addClickHandler(this);
		
		addHandlers();
	}

	private void addHandlers() {
		eventBus.addHandler(UserEnteredToGroupEvent.TYPE, new UserEnteredToGroupHandler() {
			@Override
			public void onUserEnteredToGroup(final AccountGroup acGroup) {
				if(board == null) {
					boardsManager.addBoard(new BoardImpl(null, "Тестовая доска"), new AsyncCallback<Board>(){
						@Override
						public void onFailure(Throwable caught) {
						}
						@Override
						public void onSuccess(Board result) {
							board = result;
							attachGroupToBoard(acGroup.getGroupIdentity());
							
							// Recent docs gadget
							Gadget rd = new GadgetImpl( RecentDocumentsGadgetInfo.CANONICAL_NAME );
							rd.setCel(1);
							rd.setRow(0);
							
							// Ads gadget
							Gadget ad = new GadgetImpl( AdsGadgetInfo.CANONICAL_NAME );
							ad.setCel(0);
							ad.setRow(0);
							
							// Attach gadgets
							boardsManager.attachGadgetToBoard(board.getId(), rd, new AsyncCallback<Gadget>(){
								@Override
								public void onFailure(Throwable caught) {}
								@Override
								public void onSuccess(Gadget result) {}
							});
							boardsManager.attachGadgetToBoard(board.getId(), ad, new AsyncCallback<Gadget>(){
								@Override
								public void onFailure(Throwable caught) {}
								@Override
								public void onSuccess(Gadget result) {}
							});
						}
					});
				}	else {
					attachGroupToBoard(acGroup.getGroupIdentity());
				}
			}
		});
	}
	
	private void attachGroupToBoard(GroupIdentity g) {
		boardsManager.attachGroupToBoard(board.getId(), g.getId(), new AsyncCallback<GroupIdentity>(){
			@Override
			public void onFailure(Throwable caught) {
			}
			@Override
			public void onSuccess(GroupIdentity result) {	
			}
		});
	}

	@Override
	public void onClick(ClickEvent event) {
		super.onClick(event);
		Object source = event.getSource();
		
		if(source.equals( ((Display)display).getNextButton() )) {
			// TODO: To next step when it implemented
			lobby.closeLobby();
		}	else if(source.equals( ((Display)display).getCloseButton() )) {
			lobby.closeLobby();
		}
	}
	
	@Override
	public boolean validateStep() {
		return true;
	}
}
